package com.qishanor.system.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qishanor.framework.constant.CacheConstant;
import com.qishanor.framework.util.R;
import com.qishanor.system.mapper.SysDictItemMapper;
import com.qishanor.system.mapper.SysDictMapper;
import com.qishanor.system.model.SysDict;
import com.qishanor.system.model.SysDictItem;
import com.qishanor.system.model.SysUser;
import com.qishanor.system.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: 字典表 服务实现类
 * @author: 周振林
 * @date: 2022-04-12
 * @Copyright: 博客：http://www.zhouzhenlin.com - 沉淀、分享、成长、让自己和他人都有所收获
 **/

@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    @Autowired
    private SysDictItemMapper sysDictItemMapper;




    /**
     * 根据ID 删除字典
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = CacheConstant.DICT_DETAIL, allEntries = true)
    public R removeDict(String id) {
        SysUser sysUser=(SysUser) StpUtil.getSession().get(CacheConstant.USER_DETAIL);
        if(sysUser.getIsSuper()!=1){
            return R.failed("只有超级管理员才具有删除权限");
        }

        baseMapper.deleteById(id);
        sysDictItemMapper.delete(Wrappers.lambdaQuery(SysDictItem.class).eq(SysDictItem::getDictId, id));
        return R.ok();
    }
}
